// pages/cart/cart.js
Page({
  data: {
    Address:{},
    cart:[],
    account:0,
    num:0,
    selectAll:false
  },
  slideButtonTap(e) {
    console.log('slide button tap', e.detail)
  },
  onShow: function () {
    const address = wx.getStorageSync("address");
    this.setData({Address:address});
    let cart = wx.getStorageSync("cart") || []; 
    this.setData({cart});
    this.calculate();
  },
  addAddress(){
    // 获取地址
    wx.chooseAddress({
      success: (res) => {
        // 保存缓存中
        wx.setStorageSync("address", res); 
      }
    });
    // 获取用户设置权限
    wx.getSetting({
      success: (res) => {
        let scopeAddress = res.authSetting["scope.address"];
        if (scopeAddress===false) {
          // 地址权限被关闭，引导开启
          wx.openSetting({
            success: (result) => {
              if(result.authSetting["scope.address"]===true){
                wx.chooseAddress({
                  success: (res) => {
                    wx.setStorageSync("address", res);
                  }
                });
              }
            }
          });  
        }  
      },
    });   
  },
  // 选中商品
  checkChange(e){
    const [ischeck] = e.detail.value;
    const {index} = e.currentTarget.dataset;
    this.data.cart[index].checked = ischeck==="check" ? true : false;
    this.setData({cart:this.data.cart});
    wx.setStorageSync("cart", this.data.cart);
    this.calculate();
  },
  // 计算总价格和总商品数
  calculate(){
    let sum = 0,num = 0,selectAll = true;
    if (this.data.cart.length === 0) {
      selectAll = false;
    }else{
      this.data.cart.forEach(item => {
        if (item.checked === true) {
          sum += item.goods_price * item.num;
          num += item.num;
        }else{
          selectAll = false;
        }
      });
    }
    this.setData({account:sum,num:num,selectAll:selectAll});
  },
  // 数量减少
  reduce(e){
    const {index} = e.currentTarget.dataset;
    if (this.data.cart[index].num === 1) {
      wx.showToast({
        title: '该宝贝不能减少了哟～',
        icon: 'none',
        duration: 1500,
        mask: false,
      });
      return;
    }
    this.data.cart[index].num -= 1;
    this.setData({cart:this.data.cart});
    wx.setStorageSync("cart", this.data.cart);
    if (this.data.cart[index].checked === true) {
      this.calculate();
    }
  },
  // 数量增加
  increase(e){
    const {index} = e.currentTarget.dataset;
    this.data.cart[index].num += 1;
    this.setData({cart:this.data.cart});
    wx.setStorageSync("cart", this.data.cart);
    if (this.data.cart[index].checked === true) {
      this.calculate();
    }
  },
    
  // 全选
  selectAll(e){
    const [sellect_all] = e.detail.value;
    if (sellect_all === "true") {
      this.data.cart.forEach(item => {
        item.checked = true;
      });
    }else{
      this.data.cart.forEach(item => {
        item.checked = false;
      });
    }
    this.setData({cart:this.data.cart});
    this.calculate();
  },
  // 删除宝贝
  delete(e){
    const {index} = e.currentTarget.dataset;
    wx.showModal({
      title: '提示',
      content: '确认要删除该宝贝?',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: (result) => {
        if (result.confirm) {
          this.data.cart.splice(index,1);
          this.setData({cart:this.data.cart});
          wx.setStorageSync("cart", this.data.cart);
          this.calculate();
        }
      }
    });  
  },
  settlement(){
    if (!this.data.num) {
      wx.showToast({
        title: '你还没有选购商品',
        icon: 'none',
        duration: 1500,
        mask: true,
      });
      return;
    }
    if (!this.data.Address.userName) {
      wx.showToast({
        title: '你还没有选择收货地址',
        icon: 'none',
        duration: 1500,
        mask: true,
      });
      return;
    }
    wx.navigateTo({
      url: '/pages/pay/pay',
    });
      
  }
});